//
//  ProcessingPlant.m
//  HondaPro_2
//
//  Created by Eric on 13-6-3.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "ProcessingPlant.h"
#import "MainMenuDataModel.h"
#import "UserDefinedCellForProcessingPlant.h"

@interface ProcessingPlant ()

//创建控件
-(void)createStaticImg;
-(void)initLabels;
-(void)setLabelsRect;
-(void)createSegmentButton;
-(void)createTableView;
-(void)createStaticLbls;

//刷新控件数据
-(void)freshenViewDis;

//回调函数
-(void)segmentButtonPressed:(id)sender;

@end

@implementation ProcessingPlant

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
//        [self createStaticImg];
        [self initLabels];
        [self createSegmentButton];
        [self createTableView];
        
        [self createStaticLbls];
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    [self freshenViewDis];
}

#pragma mark - 创建控件

-(void)createStaticImg
{
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"工厂处理.png"]];
    imgView.frame = CGRectMake(20, 15, 900, 660);
    
    [self addSubview:imgView];
}

-(void)initLabels
{
    self.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height+200);
    
    NSArray *arrTmp1 = [NSArray arrayWithObjects:
                        @"startTime",
                        @"completeTime",
                        @"repairPersonName",
                        @"workHour",
                        @"remarkMessage",nil];
    NSArray *arrTmp2 = [NSArray arrayWithObjects:
                        @"completeCheckDate",
                        @"completeCheckName",
                        @"remarkMessage",nil];
    arrForKeys = [NSArray arrayWithObjects:
                           @"repairPlant",
                           @"sheetMetalPlant",
                           @"sprayPainting",
                           @"completeCheck",nil];
    
    //字典里存储的是所有的关键字
    dic = [NSDictionary dictionaryWithObjectsAndKeys:
                         arrTmp1,@"repairPlant",
                         arrTmp1,@"sheetMetalPlant",
                         arrTmp1,@"sprayPainting",
                         arrTmp2,@"completeCheck", nil];
    
    //初始化所有label的rect值，以供下边使用
    [self setLabelsRect];
    
    //用循环创建label,并设置每一个label的rect
    dicForStaticLabels = [NSMutableDictionary dictionary];
    for (int i = 0; i<[arrForKeys count]; i++)
    {
        NSArray *arrForSeveralKeys = [dic objectForKey:[arrForKeys objectAtIndex:i]];
        NSMutableDictionary *dicTmp = [NSMutableDictionary dictionaryWithCapacity:[arrForSeveralKeys count]];
        NSDictionary *dicForSeveralLabelsRect = [dicForStaticLabelsRect objectForKey:[arrForKeys objectAtIndex:i]];
        
        for (int j = 0; j<[arrForSeveralKeys count]; j++)
        {
            UILabel *lbl = [[UILabel alloc] init];
            lbl.backgroundColor = [UIColor whiteColor];
            lbl.frame = [[dicForSeveralLabelsRect objectForKey:[arrForSeveralKeys objectAtIndex:j]] CGRectValue];
            lbl.numberOfLines = 10;
            lbl.font = [UIFont systemFontOfSize:13];
            lbl.textColor = [UIColor darkGrayColor];
            
            if ([[arrForSeveralKeys objectAtIndex:j] isEqualToString:@"remarkMessage"]
                &&
                [[arrForKeys objectAtIndex:i] isEqualToString:@"completeCheck"])
            {
                //添加一个背景label
                CGRect rectTmp = [self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"completeCheck"] objectForKey:@"remarkMessage"] CGRectValue] withLayOut:CGRectMake(-1, -1, 2, 2)];
                UILabel *lblTmp = [[UILabel alloc] initWithFrame:rectTmp];
                lblTmp.backgroundColor = [UIColor lightGrayColor];
                [self addSubview:lblTmp];
            }
            [self addSubview:lbl];
            [dicTmp setObject:lbl forKey:[arrForSeveralKeys objectAtIndex:j]];
        }
        [dicForStaticLabels setObject:dicTmp forKey:[arrForKeys objectAtIndex:i]];
    }
}

-(void)setLabelsRect
{
    //修理工厂的rect
    NSDictionary *dicForRepairPlant = [NSDictionary dictionaryWithObjectsAndKeys:
                                       [NSValue valueWithCGRect:CGRectMake(118, 45, 165, 30)],
                                       @"startTime",
                                       [NSValue valueWithCGRect:CGRectMake(350, 45, 175, 30)],
                                       @"completeTime",
                                       [NSValue valueWithCGRect:CGRectMake(604, 45, 124, 30)],
                                       @"repairPersonName",
                                       [NSValue valueWithCGRect:CGRectMake(795, 45, 140, 30)],
                                       @"workHour",
                                       [NSValue valueWithCGRect:CGRectMake(118, 82, 817, 30)],
                                       @"remarkMessage",nil];
    //钣金工厂的rect
    NSDictionary *dicForSheetMetalPlant = [NSDictionary dictionaryWithObjectsAndKeys:
                                           [NSValue valueWithCGRect:CGRectMake(118, 162, 165, 30)],
                                           @"startTime",
                                           [NSValue valueWithCGRect:CGRectMake(350, 162, 175, 30)],
                                           @"completeTime",
                                           [NSValue valueWithCGRect:CGRectMake(604, 162, 124, 30)],
                                           @"repairPersonName",
                                           [NSValue valueWithCGRect:CGRectMake(795, 162, 140, 30)],
                                           @"workHour",
                                           [NSValue valueWithCGRect:CGRectMake(118, 207, 817, 30)],
                                           @"remarkMessage",nil];
    //喷漆工厂的rect
    NSDictionary *dicForSprayPainting = [NSDictionary dictionaryWithObjectsAndKeys:
                                         [NSValue valueWithCGRect:CGRectMake(118, 283, 165, 30)],
                                         @"startTime",
                                         [NSValue valueWithCGRect:CGRectMake(350, 283, 175, 30)],
                                         @"completeTime",
                                         [NSValue valueWithCGRect:CGRectMake(604, 283, 124, 30)],
                                         @"repairPersonName",
                                         [NSValue valueWithCGRect:CGRectMake(795, 283, 140, 30)],
                                         @"workHour",
                                         [NSValue valueWithCGRect:CGRectMake(118, 328, 817, 30)],
                                         @"remarkMessage",nil];
    //完工检查的rect
    NSDictionary *dicForCompleteCheck = [NSDictionary dictionaryWithObjectsAndKeys:
                                         [NSValue valueWithCGRect:CGRectMake(114, 404, 150, 30)],
                                         @"completeCheckDate",
                                         [NSValue valueWithCGRect:CGRectMake(346, 404, 250, 30)],
                                         @"completeCheckName",
                                         [NSValue valueWithCGRect:CGRectMake(109, 449, 384, 200)],
                                         @"remarkMessage",nil];
    
    //字典里存储所有的rect数据
    dicForStaticLabelsRect = [NSDictionary dictionaryWithObjectsAndKeys:
                              dicForRepairPlant,@"repairPlant",
                              dicForSheetMetalPlant,@"sheetMetalPlant",
                              dicForSprayPainting,@"sprayPainting",
                              dicForCompleteCheck,@"completeCheck",nil];
}

-(void)createSegmentButton
{
    NSArray *segmentArray = [[NSArray alloc] initWithObjects:
                             @"维修前检查",
                             @"维修后检查",nil];
    segmentButton = [[UISegmentedControl alloc] initWithItems:segmentArray];
    CGRect segBtnRect = CGRectMake(600, 400, 160, 50);
    [segmentButton setFrame:segBtnRect];
    [segmentButton setMomentary:NO];
    [segmentButton setSegmentedControlStyle:UISegmentedControlStyleBar];
    [segmentButton setTintColor:[UIColor redColor]];
    [segmentButton setHighlighted:YES];
    
    [segmentButton addTarget:self action:@selector(segmentButtonPressed:) forControlEvents:UIControlEventValueChanged];
    
    segmentButton.selectedSegmentIndex = 0;
    
    [self addSubview:segmentButton];
}

-(void)createTableView
{
    tableViewDis = [[UITableView alloc] initWithFrame:CGRectMake(600, 448, 300, 200)];
    
    tableViewDis.dataSource = self;
    tableViewDis.delegate = self;
    
    tableViewDis.backgroundColor = [UIColor clearColor];
    
    [self addSubview:tableViewDis];
    
    //初始状态，默认显示的是 “维修前检查” 的数据
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    dicForDis = [modelIns.processingPlant objectForKey:@"repairBefor"];
}

-(void)createStaticLbls
{
    //修理工场
    [self createAnStaticLblByFrame:[self cgrect:CGRectMake(20, 10, 60, 30) withLayOut:CGRectMake(0, 0, 0, 0)] withTxt:@"修理工场"];
    
    //开始日期
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"repairPlant"] objectForKey:@"startTime"] CGRectValue] withLayOut:CGRectMake(-65, 0, -100, 0)] withTxt:@"开始日期:"];
    
    //完工日期
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"repairPlant"] objectForKey:@"completeTime"] CGRectValue] withLayOut:CGRectMake(-75, 0, -100, 0)] withTxt:@"完工日期:"];
    
    //维修人
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"repairPlant"] objectForKey:@"repairPersonName"] CGRectValue] withLayOut:CGRectMake(-44, 0, -80, 0)] withTxt:@"维修人:"];
    
    //作业工时
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"repairPlant"] objectForKey:@"workHour"] CGRectValue] withLayOut:CGRectMake(-60, 0, -80, 0)] withTxt:@"作业工时:"];
    
    //备注
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"repairPlant"] objectForKey:@"remarkMessage"] CGRectValue] withLayOut:CGRectMake(-65, 0, -752, 0)] withTxt:@"备注:"];
    
    //钣金工场
    [self createAnStaticLblByFrame:[self cgrect:CGRectMake(20, 125, 60, 30) withLayOut:CGRectMake(0, 0, 0, 0)] withTxt:@"钣金工场"];
    
    //开始日期
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"sheetMetalPlant"] objectForKey:@"startTime"] CGRectValue] withLayOut:CGRectMake(-65, 0, -100, 0)] withTxt:@"开始日期:"];
    
    //完工日期
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"sheetMetalPlant"] objectForKey:@"completeTime"] CGRectValue] withLayOut:CGRectMake(-75, 0, -100, 0)] withTxt:@"完工日期:"];
    
    //维修人
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"sheetMetalPlant"] objectForKey:@"repairPersonName"] CGRectValue] withLayOut:CGRectMake(-44, 0, -80, 0)] withTxt:@"维修人:"];
    
    //作业工时
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"sheetMetalPlant"] objectForKey:@"workHour"] CGRectValue] withLayOut:CGRectMake(-60, 0, -80, 0)] withTxt:@"作业工时:"];
    
    //备注
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"sheetMetalPlant"] objectForKey:@"remarkMessage"] CGRectValue] withLayOut:CGRectMake(-65, 0, -752, 0)] withTxt:@"备注:"];
    
    //喷漆工场
    [self createAnStaticLblByFrame:[self cgrect:CGRectMake(20, 245, 60, 30) withLayOut:CGRectMake(0, 0, 0, 0)] withTxt:@"喷漆工场"];
    
    //开始日期
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"sprayPainting"] objectForKey:@"startTime"] CGRectValue] withLayOut:CGRectMake(-65, 0, -100, 0)] withTxt:@"开始日期:"];
    
    //完工日期
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"sprayPainting"] objectForKey:@"completeTime"] CGRectValue] withLayOut:CGRectMake(-75, 0, -100, 0)] withTxt:@"完工日期:"];
    
    //维修人
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"sprayPainting"] objectForKey:@"repairPersonName"] CGRectValue] withLayOut:CGRectMake(-44, 0, -80, 0)] withTxt:@"维修人:"];
    
    //作业工时
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"sprayPainting"] objectForKey:@"workHour"] CGRectValue] withLayOut:CGRectMake(-60, 0, -80, 0)] withTxt:@"作业工时:"];
    
    //备注
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"sprayPainting"] objectForKey:@"remarkMessage"] CGRectValue] withLayOut:CGRectMake(-65, 0, -752, 0)] withTxt:@"备注:"];
    
    //完工检查
    [self createAnStaticLblByFrame:[self cgrect:CGRectMake(20, 370, 60, 30) withLayOut:CGRectMake(0, 0, 0, 0)] withTxt:@"完工检查"];
    
    //完工日期
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"completeCheck"] objectForKey:@"completeCheckDate"] CGRectValue] withLayOut:CGRectMake(-65, 0, -85, 0)] withTxt:@"完工日期:"];
    
    //完工检查人
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"completeCheck"] objectForKey:@"completeCheckName"] CGRectValue] withLayOut:CGRectMake(-70, 0, -180, 0)] withTxt:@"完工检查人:"];
    
    //备注
    [self createAnStaticLblByFrame:[self cgrect:(CGRect)[[[dicForStaticLabelsRect objectForKey:@"completeCheck"] objectForKey:@"remarkMessage"] CGRectValue] withLayOut:CGRectMake(-46, 0, -340, -170)] withTxt:@"备注:"];
}

#pragma mark - 刷新控件数据

-(void)freshenViewDis
{
    //更新tableView的数据
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    segmentButton.selectedSegmentIndex = 0;
    dicForDis = [modelIns.processingPlant objectForKey:@"repairBefor"];
    [tableViewDis reloadData];
    
    //更新 static label 的数据
    for (int i = 0; i<[arrForKeys count]; i++)
    {
        NSArray *arrForSeveralKeys = [dic objectForKey:[arrForKeys objectAtIndex:i]];
        for (int j = 0; j<[arrForSeveralKeys count]; j++)
        {
            NSString *str = [[modelIns.processingPlant objectForKey:[arrForKeys objectAtIndex:i]] objectForKey:[arrForSeveralKeys objectAtIndex:j]];
            UILabel *lbl = [[dicForStaticLabels objectForKey:[arrForKeys objectAtIndex:i]] objectForKey:[arrForSeveralKeys objectAtIndex:j]];
            lbl.text = str;
        }
    }
}

#pragma mark - 回调函数

-(void)segmentButtonPressed:(id)sender
{
    NSLog(@"segmentButtonPressed !!!");
    
    NSInteger selectedSegment = segmentButton.selectedSegmentIndex;
    NSLog(@"segment %i selected",selectedSegment);
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSDictionary *dicTmp = modelIns.processingPlant;
    
    switch (selectedSegment)
    {
        case 0:
            dicForDis = [dicTmp objectForKey:@"repairBefor"];
            break;
        case 1:
            dicForDis = [dicTmp objectForKey:@"repairAfter"];
            
        default:
            break;
    }
    
    [tableViewDis reloadData];
}

#pragma mark - tableView DataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSDictionary *dicTmp = modelIns.processingPlant;
    
    return 4 + MAX([[dicTmp objectForKey:@"repairBefor"] count], [[dicTmp objectForKey:@"repairAfter"] count]);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UserDefinedCellForProcessingPlant *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (nil == cell)
    {
        cell = [[UserDefinedCellForProcessingPlant alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    NSInteger row = [indexPath row];
    
    NSArray *arr1 = [NSArray arrayWithObjects:
                    @"OO检测值",
                    @"HC检测值",
                    @"RB检测值",
                    @"尾气检测值",nil];
    
    NSArray *arr2 = [NSArray arrayWithObjects:
                     @"HCdetection",
                     @"OOdetection",
                     @"RBdetection",
                     @"gasdetection",nil];
    
    //cell的风格是：浅浅灰色和浅灰色间隔出现，第一行是浅灰色
    switch (row%2)
    {
        case 0:
            //浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
            break;
        case 1:
            //浅浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1];
            break;
            
        default:
            break;
    }
    
    if (row < [dicForDis count])
    {
        cell.label1.text = [arr1 objectAtIndex:row];
        cell.label2.text = [dicForDis objectForKey:[arr2 objectAtIndex:row]];
    }
    else
    {
        cell.label1.text = cell.label2.text = @"";
    }
    
    return cell;
}

#pragma mark - tableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

//给一个CGrect加上一个偏移量
-(CGRect)cgrect:(CGRect)xingCanRect withLayOut:(CGRect)xingCanRectLayOut
{
    return CGRectMake(xingCanRect.origin.x+xingCanRectLayOut.origin.x, xingCanRect.origin.y+xingCanRectLayOut.origin.y, xingCanRect.size.width+xingCanRectLayOut.size.width, xingCanRect.size.height+xingCanRectLayOut.size.height);
}

//创建一个静态lbl
-(void)createAnStaticLblByFrame:(CGRect)xingCanRect withTxt:(NSString *)strTxt
{
    UILabel *lbl = [[UILabel alloc] initWithFrame:xingCanRect];
    lbl.backgroundColor = [UIColor clearColor];
    lbl.font = [UIFont systemFontOfSize:13];
    lbl.textColor = [UIColor darkGrayColor];
    lbl.text = strTxt;
    lbl.textAlignment = UITextAlignmentRight;
    [self addSubview:lbl];
}

@end
